package com.jzo2o.orders.dispatch.strategys;

import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.orders.dispatch.model.dto.ServeProviderDTO;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 规则抽象类
 */
public abstract class AbstractProcessRule implements IProcessRule {

    private IProcessRule next;

    public AbstractProcessRule(IProcessRule next) {
        this.next = next;
    }

    @Override
    public List<ServeProviderDTO> filter(List<ServeProviderDTO> serveProviderDTOS) {
        List<ServeProviderDTO> result = this.doFilter(serveProviderDTOS);

        //如果没有下一级规则,直接返回结果
        if (next == null) {
            return result;
        }
        if (CollUtils.size(result) <= 1) {
            // 选出唯一的服务者了,直接返回
            return result;
        }

        // 选不出唯一的,就调下一个规则进行筛选
        return next.filter(result);
    }

    public abstract List<ServeProviderDTO> doFilter(List<ServeProviderDTO> serveProviderDTOS);
}